@solid-primitives/static-store
Primitives for creating small reactive objects that doesn't change their shape over time - don't need a proxy wrapper.
Installation
npm install @solid-primitives/static-store
yarn add @solid-primitives/static-store
pnpm add @solid-primitives/static-store
createStaticStore
Creates read-only object that is shallowly reactive — only reactive on its first level and for the enumerable properties specified in the initial value — and a setter. It behaves similarly to createStore, but with limited features to keep it simple and performant. Designed to be used for reactive objects with static keys and dynamic values, like reactive Event State, location, etc.
How to use it
It takes a single argument - an initial value. It returns a tuple with the store object and a setter function.
import { createStaticStore } from "@solid-primitives/static-store";
const [size, setSize] = createStaticStore({ width: 0, height: 0 });
createEffect(() => {
console.log(size.width, size.height);
});
setSize("width", 100);
el.addEventListener("resize", () => {
setSize({ width: el.offsetWidth, height: el.offsetHeight });
});
setSize("new-property", "value");
createDerivedStaticStore
A derived version of the createStaticStore
. It will use the update function to derive the value of the store. It will only update when the dependencies of the update function change.
How to use it
It works similarly to the createMemo
primitive and it takes the same arguments, but it returns a reactive object rather than an accessor function.
const [size, setSize] = createSignal({ width: 0, height: 0 });
const store = createDerivedStaticStore(size);
createEffect(() => {
console.log(store.width, store.height);
});
el.addEventListener("resize", () => {
setSize(p => ({ ...p, height: el.offsetHeight }));
});
createHydratableStaticStore
A "hydratable" version of the createStaticStore
- it will use the serverValue
on the server and the update
function on the client. If initialized during hydration it will use serverValue
as the initial value and update it once hydration is complete.
Warning This primitive version is experimental, and mostly used internally by other primitives. It is not recommended to use it directly.
import { createHydratableStaticStore } from "@solid-primitives/static-store";
const getSize = () => ({ width: el.offsetWidth, height: el.offsetHeight });
const [size, setSize] = createHydratableStaticStore(
{ width: 0, height: 0 },
() => {
el.addEventListener("resize", () => setSize(getSize()));
return getSize();
},
);
createEffect(() => {
console.log(size.width, size.height);
});
Changelog
See CHANGELOG.md